import javax.swing.JOptionPane;

import sparrow.common.Client;
import sparrow.common.TopicHistory;
import sparrow.ui.BuddyListWindow;
import sparrow.ui.nox.*;

public class Sparrow {
	public static final String SERVER = "136.152.171.132";
	public static final int PORT = 5222;
	
	private static BuddyListWindow blw;
	
	public static void main(String[] args) {
		
		LoginDialog login = new LoginDialog();
		String action = (String)login.showDialog();
		if(action.equals(LoginDialog.CANCELCMD))
			System.exit(0);

		String email = login.getUsername();
		String pass = new String(login.getPassword());
	    
	    /*String email = "username@email.com.cn";
	    String pass = "password";*/
	    
		Client client = new Client(email, pass);
		if (client.loginTo(SERVER, PORT)) {
			TopicHistory th = new TopicHistory(email);
			
		    //requests buddy list every 90 seconds
            Thread buddyListRequester = new Thread(new Runnable(){
                public void run() {
                    System.out.println("Keep requesting buddylist...");
                    while(true){
                        try{
//                            getBuddyList();
                        } catch (Exception e){
                            e.printStackTrace();
                        }
                        if(blw != null)
                            blw.updateBuddyListPane();
                        // it's said that the buddy list is updated every 3 minutes at the server end.
                        // we refresh the buddy list every 1.5 minutes
                        try {
                            Thread.sleep(60 * 1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
//            buddyListRequester.start();
            
			//Init GUI
//			blw = new BuddyListWindow();
            JOptionPane.showMessageDialog(
            		null,"You are successfully logged in",
            		"Logged In",
            		JOptionPane.INFORMATION_MESSAGE);
            client.sendMessage("vu", "hi");
			
		} else {
			JOptionPane.showMessageDialog(
					null,"Not logged in, please check your password",
					"Not Logged In",
					JOptionPane.ERROR_MESSAGE);
		}
	}
}
